﻿using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.RepositoryProviders
{
    public static class TeamVersionRepositoryExtensions
    {
        public static IEnumerable<string> GetAutoCompleteList(this IDataRepository<TeamVersion> teamVersionRepository, IPrincipal user, string searchText)
        {
            var teamVersions = from t in teamVersionRepository
                               where !t.IsMarkedForDeletion
                               && t.TeamName.Contains(searchText)
                               select t;

            return teamVersions.AsEnumerable().Where(c => c.CanView(user)).Select(c => c.TeamName).Distinct().OrderBy(c => c);
        }

        public static IEnumerable<ICodePickerData> GetCodePickerData(this IDataRepository<TeamVersion> teamVersionRepository)
        {
            return teamVersionRepository
                .Where(c => c.IsApproved && !c.IsMarkedForDeletion)
                .OrderBy(c => c.TeamName)
                .AsEnumerable()
                .Select(c =>
                    new GuidCodePicker()
                    {
                        Code = c.TeamGuid,
                        Description = c.TeamName
                    });
        }

        public static TeamVersion SearchByPrimaryKey(this IDataRepository<TeamVersion> teamVersionRepository, Guid id)
        {
            var teamVersions = from t in teamVersionRepository
                               where t.Pk == id
                               select t;

            return teamVersions.Any() ? teamVersions.Single() : null;
        }
    }
}